package com.luck.emp.sale.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.entity.EmpTSlOrderD;
import com.luck.emp.entity.EmpTSlOrderM;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.sale.logic.ISaleLogic;
import com.luck.emp.sale.qry.SaleQry;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class SaleAction extends BaseActionSupport {
	/**
	 * 逻辑处理类
	 */
	ISaleLogic saleLogic;
	IWhLogic whLogic;
	/**
	 * 查询对象
	 */
	SaleQry qry;
	/**
	 * 分页对象
	 **/
	Page page;
	/**
	 * 实体类
	 */
	EmpTSlOrderM entity;
	List<EmpTWarehouse> whList;
	EmpTWarehouse defaultWh;// 默认仓库
	String[] productCodes;// 商品数组
	String[] priceReals;// 实际销售价
	String[] qtys;// 数量数组
	List<EmpTSlOrderD> slds;//销售单内件

	
	public String selectProduct() throws Exception{
		return null;
	}
	/**
	 * 列表
	 */
	public String list() throws Exception {
		UserBean userInfo = super.getUserInfo();

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (SaleQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new SaleQry();
		}
		if(qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
		if(qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
		qry.setUserInfo(userInfo);
		page = saleLogic.findSalePage(qry);
		return "list";
	}

	/**
	 * 新增
	 **/
	public String toAdd() throws Exception {
		UserBean userInfo = super.getUserInfo();
		entity = new EmpTSlOrderM();
		entity.setBillType("21");//普通销售单
		defaultWh =whLogic.findDefaultWarehouse(userInfo.getDeptId());
		whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
		super.event = "ADD";
		super.title = "新增销售管理";
		return "edit";
	}

	/**
	 * 修改动作
	 **/
	public String edit() throws Exception {
		UserBean userInfo = super.getUserInfo();
		String str = "";
		try {
			entity.setSlCode(null);
			entity.setBillType("21");//普通销售单
			str = saleLogic.saveSale(userInfo, entity,productCodes, priceReals, qtys);
		} catch (Exception e) {
			str = e.getMessage();
		}
		if(StringUtils.isNotBlank(str)){
			super.msgStr = str;
			return super.GLOBAL_ERROR;
		}

		super.redirectUrl = "/emp/sale_list.action";
		return super.GLOBAL_SUCCESS;
	}
	/**
	 * 撤销查询
	 */
	public String cancelList() throws Exception {
		UserBean userInfo = super.getUserInfo();

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (SaleQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new SaleQry();
		}
		if(qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getDate());
		if(qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getDate());
		qry.setUserInfo(userInfo);
		qry.setBillType("21");
		page = saleLogic.findSalePage(qry);
		return "cancel_list";
	}
	/**
	 * 撤销
	 **/
	public String toCancel() throws Exception {
		entity = saleLogic.get(EmpTSlOrderM.class, entity.getSlCode());
		if(entity.getBillType().equals("21"))
			slds = saleLogic.findSaleds(entity.getSlCode());
		super.event = "CANCEL";
		super.title = "销售撤销";
		return "cancel";
	}

	/**
	 * 撤销
	 **/
	public String cancel() throws Exception {
		UserBean userBean = super.getUserInfo();
		String str = "";
		try {
			EmpTSlOrderM bean = saleLogic.get(EmpTSlOrderM.class, entity.getSlCode());
			if(bean.getBillType().equals("21"))
				str = saleLogic.saveCancelSale(userBean, entity);
		} catch (Exception e) {
			str = e.getMessage();
		}
		if(StringUtils.isNotBlank(str)){
			super.renderJsonError(str);
		}else
			super.renderJsonSuccess("撤销成功!");
		return null;
	}

	/**
	 * 撤销查询
	 */
	public String thList() throws Exception {
		UserBean userInfo = super.getUserInfo();

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (SaleQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new SaleQry();
		}
		if(qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
		if(qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -1)));
		qry.setUserInfo(userInfo);
		page = saleLogic.findSalePage(qry);
		return "th_list";
	}
	/**
	 * 退货
	 **/
	public String toTh() throws Exception {
		entity = saleLogic.get(EmpTSlOrderM.class, entity.getSlCode());
		slds = saleLogic.findSaleds(entity.getSlCode());
		super.event = "EDIT";
		super.title = "销售订单退货";
		return "th";
	}

	/**
	 * 退货
	 **/
	public String th() throws Exception {
		UserBean userInfo = super.getUserInfo();
		String str = "";
		try {
			str = saleLogic.saveThSale(userInfo, qry, productCodes, priceReals, qtys);
		} catch (Exception e) {
			str = e.getMessage();
		}
		if(StringUtils.isNotBlank(str)){
			super.msgStr = str;
			return super.GLOBAL_ERROR;
		}
		super.redirectUrl = "/emp/sale_thList.action";
		return super.GLOBAL_SUCCESS;
	}

	/**
	 * 查看
	 **/
	public String toView() throws Exception {
		UserBean userInfo = super.getUserInfo();
		entity = saleLogic.get(EmpTSlOrderM.class, entity.getSlCode());
		slds = saleLogic.findSaleds(entity.getSlCode());
		super.title = "销售单详细信息";
		return "view";
	}

	public String print() throws Exception {
		entity = saleLogic.get(EmpTSlOrderM.class, entity.getSlCode());
		slds = saleLogic.findSaleds(entity.getSlCode());
		super.title = "销售单详细信息";
		return "print";
	}
	public SaleQry getQry() {
		return this.qry;
	}

	public void setQry(SaleQry qry) {
		this.qry = qry;
	}

	public ISaleLogic getSaleLogic() {
		return this.saleLogic;
	}

	public void setSaleLogic(ISaleLogic saleLogic) {
		this.saleLogic = saleLogic;
	}

	public Page getPage() {
		return this.page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public EmpTSlOrderM getEntity() {
		return entity;
	}

	public void setEntity(EmpTSlOrderM entity) {
		this.entity = entity;
	}

	public IWhLogic getWhLogic() {
		return whLogic;
	}

	public void setWhLogic(IWhLogic whLogic) {
		this.whLogic = whLogic;
	}

	public List<EmpTWarehouse> getWhList() {
		return whList;
	}

	public void setWhList(List<EmpTWarehouse> whList) {
		this.whList = whList;
	}

	public EmpTWarehouse getDefaultWh() {
		return defaultWh;
	}

	public void setDefaultWh(EmpTWarehouse defaultWh) {
		this.defaultWh = defaultWh;
	}

	public String[] getProductCodes() {
		return productCodes;
	}

	public void setProductCodes(String[] productCodes) {
		this.productCodes = productCodes;
	}


	public String[] getPriceReals() {
		return priceReals;
	}
	public void setPriceReals(String[] priceReals) {
		this.priceReals = priceReals;
	}
	public String[] getQtys() {
		return qtys;
	}

	public void setQtys(String[] qtys) {
		this.qtys = qtys;
	}
	public List<EmpTSlOrderD> getSlds() {
		return slds;
	}
	public void setSlds(List<EmpTSlOrderD> slds) {
		this.slds = slds;
	}

}